package KUtilities;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.URI;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public abstract class KAbout extends JMenuItem {
	private static final long serialVersionUID = 1L;
	JMenuItem about;
	
	public KAbout() {
		super("About");
		setMnemonic('A');
	}
	
	public JPanel aboutDeveloper(){
		JPanel aboutPanel=new JPanel();
		aboutPanel.setOpaque(false);
		KSpringOld aboutSpring=new KSpringOld(aboutPanel);
		
		final JLabel developedBy,contact;
		final JButton gPlus,fb;
		
		developedBy=new JLabel("Developer: GeethaNadh Padavala");
		contact=new JLabel("Contact:");
		gPlus=new JButton("On Google+");
		fb=new JButton("On Facebook");
		
		Toolkit tk = Toolkit.getDefaultToolkit();
	    Image hand = tk.getImage("lib"+File.separator+"images"+File.separator+"hand.gif");
		Cursor handCursor= tk.createCustomCursor(hand, new Point(10,7), "Hand");
				
		fb.setCursor(handCursor);
		gPlus.setCursor(handCursor);
		developedBy.setForeground(Color.green);
		contact.setForeground(Color.yellow);
		
		gPlus.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(Desktop.isDesktopSupported()){
					try {
						Desktop.getDesktop().browse(new URI("https://plus.google.com/112466599557864128316/about"));
					} catch (Exception e) {}
				}
			}
		});
		
		fb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(Desktop.isDesktopSupported()){
					try {
						Desktop.getDesktop().browse(new URI("http://www.facebook.com/gnadh2007"));
					} catch (Exception e) {}
				}
			}
		});
				
		aboutSpring.add(developedBy,contact,gPlus,fb);
		aboutSpring.setHGap(5, aboutPanel,developedBy,aboutPanel);
		aboutSpring.setVGap(5, aboutPanel,developedBy,contact,gPlus,fb,aboutPanel);
		aboutSpring.alignLeft(developedBy,contact,gPlus,fb);
		
		return aboutPanel;
	}
	
	public abstract JPanel aboutApp();
	
	Timer timer;
	ActionListener timerAction;
	public ActionListener getAboutAction(){
		final KPanel aboutPanel=new KPanel();
				
		aboutPanel.setOpaque(false);
		KSpringOld aboutSpring=new KSpringOld(aboutPanel);
		JPanel aboutApp=aboutApp();
		JPanel aboutDev=aboutDeveloper();
		
		aboutSpring.add(aboutApp,aboutDev);
		aboutSpring.setHGap(5,aboutPanel,aboutApp,aboutPanel);
		aboutSpring.setVGap(5,aboutPanel,aboutApp,aboutDev,aboutPanel);
		
		ActionListener action=new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				final JDialog dialog=new JDialog(KSwing.gui,true);
				JLayeredPane main;
				KSpringOld mainSpring;
				main=new JLayeredPane();
				mainSpring=new KSpringOld(main);
				File img=new File("lib"+File.separator+"images"+File.separator+"background.jpg");
				JPanel background=KSwing.getBackGroundPanel(img,KSwing.gui.getRootPane().getWidth(),KSwing.gui.getRootPane().getHeight());
				JPanel transparent=KSwing.createTransparentPanel(KSwing.gui.getRootPane().getWidth(),KSwing.gui.getRootPane().getHeight());
				mainSpring.addToCenter(background,transparent,aboutPanel);
				dialog.add(main);
				dialog.setUndecorated(true);
				dialog.pack();
				dialog.setLocation(100,100);
				dialog.setLocationRelativeTo(KSwing.gui.getRootPane());
				
				KSwing.disable(dialog);
				aboutPanel.zoomIn(200);
				timerAction=new ActionListener(){
					public void actionPerformed(ActionEvent ae)
					{
						if(!aboutPanel.isAlive())
						{
							timer.stop();
							KSwing.enable(dialog);
						}
					}
				};
				timer=new Timer(30,timerAction);
				timer.start();
				AbstractAction escapeKey=new AbstractAction(){
					private static final long serialVersionUID = 1L;
					public void actionPerformed(ActionEvent ae)
					{
						KSwing.disable(dialog);
						aboutPanel.zoomOut(200);
						timerAction=new ActionListener(){
							public void actionPerformed(ActionEvent ae)
							{
								if(!aboutPanel.isAlive())
								{
									timer.stop();
									timer=null;
									timerAction=null;
									aboutPanel.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
									dialog.dispose();
								}
							}
						};
						timer=new Timer(30,timerAction);
						timer.start();
					}
				};
				aboutPanel.registerKeyboardAction(escapeKey, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),JComponent.WHEN_IN_FOCUSED_WINDOW);
				dialog.setVisible(true);
			}
		};
		return action;
	}
}
